package models;

import java.util.List;

import javax.persistence.Entity;

import jiyici.Constant.ModelColumn;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

import play.db.ebean.Model;

@Entity
public class ItemParam extends ServerBaseModel {

    private static final long serialVersionUID = -4198772283620977840L;

    // @ManyToOne
    // @JoinColumn(name = "item_id")
    // public Item item;
    public long itemId;

    public String itemParamName;

    public int paramType;

    public boolean isMust;
    
    public String displayOrder;

    @Override
    public ObjectNode toJson() {
        ObjectNode node = super.toJson();
        node.put(ModelColumn.ITEM_PARAM_COLUMN_ITEMID, itemId);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_NAME, itemParamName);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_TYPE, paramType);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_IS_MUST, isMust);
        node.put(ModelColumn.ITEM_PARAM_COLUMN_DISPLAY_ORDER, displayOrder);
        return node;
    }

    @Override
    public void fromJson(JsonNode json) {
        super.fromJson(json);
        itemId = json.findPath(ModelColumn.ITEM_PARAM_COLUMN_ITEMID).getLongValue();
        itemParamName = json.findPath(ModelColumn.ITEM_PARAM_COLUMN_NAME).getTextValue();
        paramType = json.findPath(ModelColumn.ITEM_PARAM_COLUMN_TYPE).getIntValue();
        isMust = json.findPath(ModelColumn.ITEM_PARAM_COLUMN_IS_MUST).getBooleanValue();
        displayOrder = json.findPath(ModelColumn.ITEM_PARAM_COLUMN_DISPLAY_ORDER).getTextValue();
    }

    public static Model.Finder<Long, ItemParam> find = new Model.Finder<Long, ItemParam>(Long.class, ItemParam.class);

    public static List<ItemParam> modifiedAfter(User user, long time) {
        return find.where().eq("modelId.userId", user.id).gt(ModelColumn.BASE_COLUMN_LASTMODIFIED, time)
                .findList();
    }
}
